/**
 * Created by dqw on 2015/12/30.
 */

var LoadFormStat = function () {
    function _loadForm(startTimeStr,endTimeStr,agentId) {
        $.ajax({
            type: 'POST',
            url:  ncGlobal.adminRoot + 'order/json/form',
            data: {
                "startTimeStr":startTimeStr,
                "endTimeStr":endTimeStr,
                "agentId":agentId
            },
            success:function(req){
                console.log(req);
                if(req.code =='200'){
                    var tUser  = 0;
                    var tNumBuy  = 0;
                    var tProBuy  = 0;
                    var tNumFol  = 0;
                    var tProFol  = 0
                    var tPen  = 0;
                    var tFee  = 0;
                    var tdiv  = 0;
                    for(var i= 0; i< req.data.length;i++){
                        var data =   req.data[i]
                        if(data.code=='GC'){
                            $("#userGC").text(data.sumUser);
                            $("#numBuyGC").text(data.sumBuyNum);
                            $("#proBuyGC").text(data.sumBuyProfit);
                            $("#numFolGC").text(data.sumFollowNum);
                            $("#proFolGC").text(data.sumFollowProfit);

                            $("#penGC").text(data.sumPenetrate);
                            $("#feeGC").text(data.sumFee);
                            $("#divGC").text(data.sumDivided);
                        }else if (data.code=='CL'){
                            $("#userCL").text(data.sumUser);
                            $("#numBuyCL").text(data.sumBuyNum);
                            $("#proBuyCL").text(data.sumBuyProfit);
                            $("#numFolCL").text(data.sumFollowNum);
                            $("#proFolCL").text(data.sumFollowProfit);
                            $("#penCL").text(data.sumPenetrate);
                            $("#feeCL").text(data.sumFee);
                            $("#divCL").text(data.sumDivided);
                        }else if (data.code=='SI'){
                            $("#userSI").text(data.sumUser);
                            $("#numBuySI").text(data.sumBuyNum);
                            $("#proBuySI").text(data.sumBuyProfit);
                            $("#numFolSI").text(data.sumFollowNum);
                            $("#proFolSI").text(data.sumFollowProfit);
                            $("#penSI").text(data.sumPenetrate);
                            $("#feeSI").text(data.sumFee);
                            $("#divSI").text(data.sumDivided);
                        }else if (data.code=='HSI'){
                            $("#userHSI").text(data.sumUser);
                            $("#numBuyHSI").text(data.sumBuyNum);
                            $("#proBuyHSI").text(data.sumBuyProfit);
                            $("#numFolHSI").text(data.sumFollowNum);
                            $("#proFolHSI").text(data.sumFollowProfit);
                            $("#penHSI").text(data.sumPenetrate);
                            $("#feeHSI").text(data.sumFee);
                            $("#divHSI").text(data.sumDivided);
                        }else if (data.code=='MHI'){
                            $("#userMHI").text(data.sumUser);
                            $("#numBuyMHI").text(data.sumBuyNum);
                            $("#proBuyMHI").text(data.sumBuyProfit);
                            $("#numFolMHI").text(data.sumFollowNum);
                            $("#proFolMHI").text(data.sumFollowProfit);
                            $("#penMHI").text(data.sumPenetrate);
                            $("#feeMHI").text(data.sumFee);
                            $("#divMHI").text(data.sumDivided);
                        }else if (data.code=='DAX'){
                            $("#userDAX").text(data.sumUser);
                            $("#numBuyDAX").text(data.sumBuyNum);
                            $("#proBuyDAX").text(data.sumBuyProfit);
                            $("#numFolDAX").text(data.sumFollowNum);
                            $("#proFolDAX").text(data.sumFollowProfit);
                            $("#penDAX").text(data.sumPenetrate);
                            $("#feeDAX").text(data.sumFee);
                            $("#divDAX").text(data.sumDivided);
                        }
                        tUser += data.sumUser;
                        tNumBuy += data.sumBuyNum
                        tProBuy += data.sumBuyProfit
                        tNumFol += data.sumFollowNum;
                        tProFol += data.sumFollowProfit;
                        tPen += data.sumPenetrate;
                        tFee += data.sumFee;
                        tdiv += data.sumDivided;
                    }
                    $("#totalUser").text(tUser);
                    $("#totalNumBuy").text(tNumBuy);
                    $("#totalProBuy").text(tProBuy);
                    $("#totalNumFol").text(tNumFol);
                    $("#totalProFol").text(tProFol);
                    $("#totalPen").text(tPen);
                    $("#totalFee").text(tFee);
                    $("#totalDiv").text(tdiv);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.ncAlert({
                    content: '<div class="alert alert-danger m-b-0"><h4><i class="fa fa-info-circle"></i>&nbsp;连接超时</h4></div>',
                    autoCloseTime: 3,
                    callback: function () {
                    }
                });
            }
        });
    }

    function _loadForm2(startTimeStr,endTimeStr,agentId) {
        $.ajax({
            type: 'POST',
            url:  ncGlobal.adminRoot + 'order/json/other',
            data: {
                "startTimeStr":startTimeStr,
                "endTimeStr":endTimeStr,
                "agentId":agentId
            },
            success:function(req){
                console.log(req);
                if(req.code =='200'){
                    $("#defray").text(req.data.defray)
                    $("#availableBuy").text(req.data.availableBuy)
                    $("#freezeBuy").text(req.data.freezeBuy)
                    $("#availableInv").text(req.data.availableInv)
                    $("#freezeInv").text(req.data.freezeInv)
                    $("#quickAmo").text(req.data.quickAmo)
                    $("#baoAmo").text(req.data.baoAmo)
                    $("#otherAmo").text(req.data.otherAmo)
                    $("#cashAmo").text(req.data.cashAmo)
                    $("#cashNum").text(req.data.cashNum)
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.ncAlert({
                    content: '<div class="alert alert-danger m-b-0"><h4><i class="fa fa-info-circle"></i>&nbsp;连接超时</h4></div>',
                    autoCloseTime: 3,
                    callback: function () {
                    }
                });
            }
        });
    }

    //外部可调用
    return {
        init:function (startTimeStr,endTimeStr,agentId) {
            loadForm:_loadForm(startTimeStr,endTimeStr,agentId);
            loadForm2: _loadForm2(startTimeStr,endTimeStr,agentId);
        }
    }
}();









function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}


//操作处理开始
var OperateHandle = function () {

    function _bindEvent() {

        // 编辑借款标对话框
        $('#editBank').on('show.bs.modal', function (event) {
            var    //获取接受事件的元素
                button = $(event.relatedTarget),
                //获取data 参数
                datano = button.data('no'),
                modal = $(this),
                //获取列表框中的原始数据
                gridData = grid.sortOriginalDatas[datano],
                editForm = $("#editForm");
            //清除错误提示
            editForm.psly().reset();
            $(".alert-danger").remove();
            modal.find('input[name="id"]').val(gridData.id);
            modal.find('input[name="branch"]').val(gridData.branch);
            modal.find('input[name="bankNo"]').val(gridData.bankNo);
            // $("#banks option[value='"+gridData.bankCode+"']").attr("selected","selected");
            $("#banks").find("option[value="+gridData.bankCode+"]").attr("selected",true);
            // $("#banks").attr("value",gridData.bankCode);
        });
    }
    function _doStat() {
        startTime = $("#startTimeStr").val();
        endTime = $("#endTimeStr").val();
        agentid = $('#agentList option:selected') .val();
        LoadFormStat.init(startTime,endTime,agentid);
    }

    /**
     * 导出
     */
    $('#exportExecute').click(function () {
        $('#exportFormContainer').remove();
        var exportFormContainer = document.createElement('div');
        var exportForm = document.createElement('form');
        var input = document.createElement('input');
        exportFormContainer.id = 'exportFormContainer';
        exportForm.id = 'exportForm';
        exportForm.method = 'post';
        exportForm.action = "/s/orderFrom/json/export";
        input.type = 'hidden';
        input.name = 'startTimeStr';
        input.value = $("#startTimeStr").val();
        var input2 = document.createElement('input');
        input2.type = 'hidden';
        input2.name = 'endTimeStr';
        input2.value =  $("#endTimeStr").val();
        var input3 = document.createElement('input');
        input3.type = 'hidden';
        input3.name = 'agentId';
        input3.value =  $('#agentList option:selected') .val();
        exportForm.appendChild(input);
        exportForm.appendChild(input2);
        exportForm.appendChild(input3);
        exportFormContainer.appendChild(exportForm);
        $('body').append(exportFormContainer);
        $("#exportForm").submit();
    });

    //外部可调用
    return {
        init: function () {
            _bindEvent();
        },
        doStat:_doStat
    }
}();
//操作处理结束

$(function () {
    //页面绑定事件
    startTimeStr = $("#startTimeStr").val();
    endTimeStr = $("#endTimeStr").val();
    agentId = $('#agentList option:selected') .val();
    OperateHandle.init();
    LoadFormStat.init(startTimeStr,endTimeStr,agentId);
});